/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.InterfaceGenerator;
import com.inspur.edp.caf.generator.item.InterfaceGeneratorContext;
import com.inspur.edp.cef.api.dataType.base.ICefDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.dataTypeInfo.DataTypeInfoGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.DataTypeResourceInfoGenerator;
import java.util.ArrayList;
import java.util.Arrays;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

//原DataTypeInterfaceGenerator与DataTypeAllInterfaceGenerator生成两个接口,
//元数据上开启选项后,使用此生成器生成上述两个接口的合并接口;
public abstract class DataTypeSingleInterfaceGenerator extends InterfaceGenerator {
  private CefApiGeneratorContext apiGenContext;
  private IGspCommonDataType dataType;

  protected DataTypeSingleInterfaceGenerator(
      CefApiGeneratorContext apiGenContext, IGspCommonDataType dataType) {
    super(apiGenContext.getCompilationUnitInfo());
    this.apiGenContext = apiGenContext;
    this.dataType = dataType;
  }

  @Override
  protected final InterfaceGeneratorContext createInterfaceItemContext() {
    return createInterfaceGenContext();
  }

  @Override
  protected void beforeInitialize() {
    super.beforeInitialize();
    MediateType type = new MediateType(apiGenContext.getPackageName(), getName());
    apiGenContext.getEntityAllInterfaceTypes().put(dataType.getCode(), type);
    apiGenContext.getEntityInterfaceTypes().put(dataType.getCode(), type);
  }

  @Override
  protected DataTypeInterfaceGeneratorContext getContext() {
    return (DataTypeInterfaceGeneratorContext) super.getContext();
  }

  protected DataTypeInterfaceGeneratorContext createInterfaceGenContext() {
    return new DataTypeInterfaceGeneratorContext(apiGenContext, dataType);
  }

  @Override
  protected ArrayList<TypeInfo> addParentInterfaces() {
    return new ArrayList(Arrays.asList(getDefaultInterfaceType()));
  }

  protected TypeInfo getDefaultInterfaceType() {
    return new TypeInfo(ICefDataType.class);
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifier() {
    return new ArrayList<>(
        java.util.Arrays.asList(new ModifierKeyword[] {ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected ArrayList<BaseGenerator> createChildGenerators() {
    ArrayList<BaseGenerator> list = super.createChildGenerators();
    if (list == null) {
      list = new ArrayList<>();
    }
    // 节点
    DataTypeResourceInfoGenerator resourceInfo = createResourceInfoGenerator();
    if(resourceInfo != null)
      list.add(resourceInfo);

    return list;
  }

  //与DataTypeInterfaceGenerator存在重复实现
  protected abstract DataTypeResourceInfoGenerator createResourceInfoGenerator();
}
